"""
Created by 饼干 on 2019/11/4 17:19
"""
from threading import Thread

from flask import current_app, render_template

from app import mail
from flask_mail import Message
__author__ = '饼干'


def send_async_email(app, msg):
    with app.app_context():
        try:
            mail.send(msg)
        except Exception as e:
            pass


def send_mail(to, subject, template, **kwargs):
    # msg = Message('测试邮件', sender='jindongkui@vip.qq.com', body='Test',
    #               recipients=['jindongkui@vip.qq.com'])
    msg = Message('鱼书' + '' + subject,
                  sender=current_app.config['MAIL_SENDER'],
                  recipients=[to])
    msg.html = render_template(template, **kwargs)
    app = current_app._get_current_object()

    thr = Thread(target=send_async_email, args=[app, msg])
    thr.start()

